package com.rbac.web.db;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.rbac.web.model.User;
import java.util.List;
import java.util.Optional;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.SuperBuilder;

/**
 * @author gengaoliu
 */
@Getter
@Setter
@ToString
@SuperBuilder
@TableName(value = "user", autoResultMap = true)
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
public class UserDO extends BaseDO {
  private String name;
  private String phone;
  private String username;
  private String password;
  private Boolean status;

  @TableField(typeHandler = JacksonTypeHandler.class)
  private List<Integer> roleId;

  public static UserDO from(User domain) {
    return UserDO.builder()
        .id(domain.getId())
        .createBy(domain.getCreatedBy())
        .createTime(domain.getCreatedTime())
        .updateBy(domain.getUpdatedBy())
        .updateTime(domain.getUpdatedTime())
        .deleted(Optional.ofNullable(domain.getDeleted()).orElse(false))
        .name(domain.getName())
        .username(domain.getUsername())
        .phone(domain.getPhone())
        .password(domain.getPassword())
        .status(Optional.ofNullable(domain.getStatus()).orElse(true))
        .roleId(domain.getRoleId())
        .build();
  }

  public User to() {
    return User.builder()
        .id(this.getId())
        .createdBy(this.getCreateBy())
        .createdTime(this.getCreateTime())
        .updatedBy(this.getUpdateBy())
        .updatedTime(this.getUpdateTime())
        .name(this.getName())
        .phone(this.getPhone())
        .username(this.getUsername())
        .password(this.getPassword())
        .status(this.getStatus())
        .roleId(this.getRoleId())
        .build();
  }
}
